package uk.ac.salfordcc.sccmobile;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

/**
 * Created by bwarrington on 06/08/13.
 */
public class PINActivity extends Activity {
    String setOne, setTwo, insertCode;
    EditText mCode;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pin);

        mCode = (EditText)findViewById(R.id.pinCode);

        ActionBar bar = getActionBar();
        bar.hide();
/*
        code.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if(i == keyEvent.KEYCODE_ENTER){
                    Log.i("scc PIN ENTER: ", "true");
                }
                return true;
            }
        });*/

        Button btn = (Button)findViewById(R.id.enterPinBtn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkPINCode(mCode.getText().toString());
            }
        });

        /** Set focus listener for the pin code TextView **/
        mCode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    /** If the pin code TextView has focus, the soft keyboard is displayed **/
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                }
            }
        });
    }

    public void checkPINCode(String code){
        //Toast.makeText(this, code, Toast.LENGTH_SHORT).show();

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = sp.edit();

        String pinCode = sp.getString("PIN_SEC", "");
        //Log.i("scc: Stored Pin - ", pinCode + "\n");
        //Log.i("scc: Entered Pin - ", code);

        if(pinCode.equals(code)){
            editor.putLong("PIN_EXP", System.currentTimeMillis()+360000);
            editor.commit();
            Log.i("scc updated PIN_EXP", "");
            finish();
        }
        else{
            mCode.setText("");
            Toast.makeText(this, "Pin Code Incorrect. Please try again.", Toast.LENGTH_SHORT).show();
        }
    }


    /** Override the standard back button press method **/
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBackPressed(){
        finishAffinity();
    }
}